package com.tianji.learning.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2024-09-02
 */
@Slf4j
@Service
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {
    /**
     * 根绝上个月的时间查询赛季id
     *
     * @param beforMonth
     * @return
     */
    @Override
    public Integer querySeasonIdByBeforMonth(LocalDateTime beforMonth) {
        //1.查询赛季id，根据时间
        //select * from points_board_season where begin_time <= '2023-09-05' and '2023-09-05' <= end_time
        PointsBoardSeason pointsBoardSeason = this.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, beforMonth)
                .ge(PointsBoardSeason::getEndTime, beforMonth)
                .one();

        //2.为空返回一个空
        if (ObjectUtil.isEmpty(pointsBoardSeason)) {
            log.error("榜单数据不存在");
            throw new BizIllegalException("历史榜单数据不存在");
        }
        //3.返回赛季id
        return pointsBoardSeason.getId();
    }
}
